/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bsd5ex.group1.kaokang;

import com.bsd5ex.group1.kaokang.dao.FoodDAO;
import com.bsd5ex.group1.kaokang.dao.OrderLineItemTypeDAO;
import com.bsd5ex.group1.kaokang.dbUtil.HibernateUtil;
import com.bsd5ex.group1.kaokang.helper.UnitTransferHelper;
import com.bsd5ex.group1.kaokang.model.Food;
import com.bsd5ex.group1.kaokang.model.Order;
import com.bsd5ex.group1.kaokang.model.OrderLineDetail;
import com.bsd5ex.group1.kaokang.model.OrderLineItem;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author IBIAINIKI
 */
public class StoreOrder {

    private Order order;
    private Set<OrderLineItem> orderLineItems;

    public StoreOrder() {
        order = new Order(new Date(), 0);
        orderLineItems = new HashSet<OrderLineItem>();
    }

    public Order getOrder() {
        return new CalculateOrder().getOrderWithCalculatePrice(order);
    }

    public void addOrderLineItem(List<Integer> foodIDs) {

        if (foodIDs == null || foodIDs.size() == 0) {
            return;
        }
        OrderLineItem orderLineItem = new OrderLineItem();
        orderLineItem.setOrder(order);
        if (foodIDs.size() == 1) {
            orderLineItem.setOrderLineItemType(new OrderLineItemTypeDAO().getOrderLineItemTypeByID(1));
        } else {
            orderLineItem.setOrderLineItemType(new OrderLineItemTypeDAO().getOrderLineItemTypeByID(2));
        }
        Set<OrderLineDetail> orderLineDetailsSet = new HashSet<OrderLineDetail>();
        for (int i = 0; i < foodIDs.size(); i++) {
            FoodDAO foodDAO = new FoodDAO();
            Food food = foodDAO.getFoodByID(foodIDs.indexOf(i));
            orderLineDetailsSet.add(new OrderLineDetail(food, orderLineItem, new UnitTransferHelper().getLiteRatio(food.getPicking().getRatio(), food.getPicking().getUnitType())));
        }
        orderLineItem.setOrderLineDetails(orderLineDetailsSet);
        orderLineItems.add(orderLineItem);
    }
    
    public void saveOrder(){
        if(order.getTotalPrice() == 0){
            order = new CalculateOrder().getOrderWithCalculatePrice(order);
        }
        // ตรงนี้ครับ

    }
}
